<%@ page contentType="text/html; charset=utf-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.util.*,java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*, dreampack.tool.*" %>

<%
	if (null != request.getParameter("rnd"))
		return;
	String chose = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char display[] = { '0', ' ', '0', ' ', '0', ' ', '0' }, ran[] = {'0', '0', '0', '0' }, temp;
    Random rand = new Random();
    for (int i = 0; i < 4; i++)
	{	
	    temp = chose.charAt(rand.nextInt(chose.length()));
	    display[i * 2] = temp;
	    ran[i] = temp;
	}
	String random = String.valueOf(display);
	session.setAttribute("ValidateCode", String.valueOf(ran));
	out.clear();
	response.setContentType("image/jpeg");
	response.addHeader("pragma", "NO-cache");
	response.addHeader("Cache-Control", "no-cache");
	response.addDateHeader("Expries", 0);

	int width = ConvertTool.SafeGetInt(request.getParameter("width"), 60);
	int height = ConvertTool.SafeGetInt(request.getParameter("height"), 20);
	
	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	Graphics g = image.getGraphics();
	//填充背景颜色
	g.setColor(Color.DARK_GRAY);
	g.fillRect(0, 0, width, height);
	//设置字体颜色
	g.setColor(Color.orange);
	width = (width - 44) / 2;
	height = (height / 2) + 6;
	g.drawString(random, width, height);	//生成的字符串宽高为：44 : 12
	g.dispose();
	ServletOutputStream outStream = response.getOutputStream();
	JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream);
	encoder.encode(image);
	outStream.close();
%>
